export default (function(){
	var GameAudio=function(url,load,succen){
		this.loader=0;
		this.succen=succen;
		this.load=load;
		this.ios=false;
		var _this=this;
		if(url.length){
			this.AudioStore=[];
			for(let i=0;i<url.length;i++){
				// 创建音频
				let dom=document.createElement("audio");
				dom.volume=0;
				dom.src=url[i];
				dom.controls=false;
				this.AudioStore.push(dom);
				dom.onload=function(){
					_this.loader+=100/url.length;
					_this.getSuccen();
				}
			}
		}else{
			this.AudioStore={};
			let i;
			for(i in url){
				let doms=document.createElement("audio");
				doms.volume=0;
				doms.src=url[i];
				doms.controls=false;
				this.AudioStore[i]=doms;
				doms.onload=function(){
					_this.loader+=100/Object.values(url).length;
					_this.getSuccen();
				}
			}
		}
		let u = window.navigator.userAgent
		this.ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // 是否为ios终端
	}
	// 返回加载状态
	GameAudio.prototype.getLoad=function(){
		return this.loader/100;
	}
	// ios音频加载
	GameAudio.prototype.isIOS = function(){
		console.log(this.ios)
		return this.ios
	}
	GameAudio.prototype.IosPlay=function(x){
		// 判断播放一段音频还是多段
		console.log("苹果系统")
			this.AudioStore[x[0]].load();
			this.AudioStore[x[0]].play();
			// window.removeEventListener('touchstart', forceSafariPlayAudio, false);
			this.AudioStore[x[0]].pause();
	}
	// 进行音频播放
	GameAudio.prototype.Play=function(x){
		// 判断播放一段音频还是多段
		if(x.length==1){
			this.AudioStore[x[0]].play();
		}else{
			var _this=this;
			this.AudioStore[x[0]].play();
			function is(){
				x.shift();
				if(x.length==0){
					return false;
				}
				_this.AudioStore[x[0]].play();
				_this.AudioStore[x[0]].onended=is;
			}
			this.AudioStore[x[0]].onended=is;
		}
	}
	// 音频加载成功回调
	GameAudio.prototype.getSuccen=function(){
		if(this.loader>=99){
			if(typeof(this.succen)=="function"){//全部加载成功回调
				this.succen();
			}
			this.getSuccen=function(){};//去除加载回调
		}else{
			if(typeof(this.load)=="function"){//加载一个音频文件的回调

				this.load();
			}
		}
	}
	//设置音量
	GameAudio.prototype.setVolume=function(x){
		if(this.AudioStore.length){
			for(let i=0;i<this.AudioStore.length;i++){
				this.AudioStore[i].volume=x/100;
			}
		}else{
			let i;
			for(i in this.AudioStore){
				this.AudioStore[i].volume=x/100;
			}
		}
	}
	return GameAudio;
}())